package cn.edu.gdgm.system.service.impl;

import cn.edu.gdgm.system.entity.Plan;
import cn.edu.gdgm.system.entity.Slot;
import cn.edu.gdgm.system.enums.ScheduleStatusEnum;
import cn.edu.gdgm.system.mapper.PlanMapper;
import cn.edu.gdgm.system.mapper.SlotMapper;
import cn.edu.gdgm.system.service.IPlanService;
import cn.edu.gdgm.system.utils.JobUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * <p>
 * 服药计划 服务实现类
 * </p>
 *
 * @author generator
 * @since 2021-02-26
 */
@Service
public class PlanServiceImpl  extends ServiceImpl<PlanMapper, Plan> implements IPlanService {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private PlanMapper baseMapper;

    @Resource
    private SlotMapper slotMapper;

    @Resource
    private JobUtils jobUtils;

    @Override
    public  IPage<Plan> findListByPage(Integer page, Integer pageCount){
        IPage<Plan> wherePage = new Page<>(page, pageCount);
        Plan where = new Plan();
        return baseMapper.selectPage(wherePage, Wrappers.query(where));
    }

    @Override
    public int add(Plan plan){
        plan.setInvokeTarget(UUID.randomUUID().toString());
        String slotId = plan.getSlotId();
        Slot slot = slotMapper.selectById(slotId);
        if (slot == null || slot.getMedicinalName() == null) {
            return 0;
        }
        jobUtils.addJob(plan, slot.getMedicinalName());
        return baseMapper.insert(plan);
    }

    @Override
    public int delete(Long id){
        Plan plan = baseMapper.selectById(id);
        String invokeTarget = plan.getInvokeTarget();

        jobUtils.deleteJob(invokeTarget);
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(Plan plan){
        return baseMapper.updateById(plan);
    }

    @Override
    public Plan findById(Long id){
        return  baseMapper.selectById(id);
    }

    @Override
    public int pause(Long id) {
        Plan plan = baseMapper.selectById(id);
        plan.setStatus(ScheduleStatusEnum.PAUSE.getValue());
        // 删除任务
        jobUtils.deleteJob(plan.getInvokeTarget());
        return baseMapper.updateById(plan);
    }

    @Override
    public int start(Long id) {
        Plan plan = baseMapper.selectById(id);
        plan.setStatus(ScheduleStatusEnum.NORMAL.getValue());
        // 添加一个新的任务
        plan.setInvokeTarget(UUID.randomUUID().toString());
        String slotId = plan.getSlotId();
        Slot slot = slotMapper.selectById(slotId);
        if (slot == null || slot.getMedicinalName() == null) {
            return 0;
        }
        jobUtils.addJob(plan, slot.getMedicinalName());
        return baseMapper.updateById(plan);
    }


}
